package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.riskamount;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.medium.PolicyRiskInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * description:
 * NPT015风险保额
 *
 * @author jackdaw
 * @date 2020-04-19 18:03
 */
@Component
public class Npt015Strategy implements InsuranceRiskAmountStrategy {
    @Override
    public BigDecimal getRiskAmount(PolicyRiskInfoModel policyRiskInfoModel, boolean isCurrent, Object object) {
        //投被保人不是同一人，且不是趸交 且年龄小于等于60
        if (!StringUtils.equals(policyRiskInfoModel.getAppntNo(), policyRiskInfoModel.getInsuredNo()) &&
                !StringUtils.equals(String.valueOf(policyRiskInfoModel.getPayIntv()), UnderWriteConstant.SINGLE_PAY)) {
            long age = DateUtil.getYears(policyRiskInfoModel.getAppntBirthday(), new Date());
            if (age < (int) object) {
                //年交保费
                BigDecimal prem = policyRiskInfoModel.getPrem();
                prem = prem.multiply(BigDecimal.valueOf(12 / policyRiskInfoModel.getPayIntv()));
                //缴费年限
                int payYears = policyRiskInfoModel.getPayYears();
                if (policyRiskInfoModel.getPayTodate() == null) {
                    return prem.multiply(BigDecimal.valueOf(payYears)).divide(BigDecimal.valueOf(3), 20, BigDecimal.ROUND_HALF_UP);
                }
                return policyRiskInfoModel.getAmnt().multiply(BigDecimal.valueOf(DateUtil.getYears(policyRiskInfoModel.getPayTodate(), policyRiskInfoModel.getPayEndDate())));
            }
        }
        return BigDecimal.valueOf(0);
    }
}
